package someTestExcemple.huaweiOd.greedyMethod;

import java.util.Arrays;
import java.util.Scanner;
//华为OD机试 - 贪心的商人 - 贪心（Java 2025 A卷 100分）
//https://blog.csdn.net/guorui_java/article/details/130630159

public class GreedyBusinessmen {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //商品种类
        int number = Integer.parseInt(sc.nextLine());
        //天数
        int days = Integer.parseInt(sc.nextLine());
        //每件商品最大持有数量
        int[] maxHoldArr = Arrays.stream(sc.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
        //priceArr[i][j]第i件商品第j天的价格
        int[][] priceArr = new int[number][days];
        for(int i=0;i<number;i++) {
            for(int j=0;j<days;j++) {
                priceArr[i][j] = sc.nextInt();
            }
        }
        sc.close();
        int maxProfitSum = 0;
        for(int i=0;i<number;i++) {
            for(int j=1;j<days;j++) {
                int cost = priceArr[i][j-1];
                int nowPrice =priceArr[i][j];
                if(nowPrice-cost > 0) {
                    maxProfitSum+= (nowPrice-cost)*maxHoldArr[i];
                }
            }
        }
        System.out.println(maxProfitSum);
    }
}
